package org.yoao.moneymachine.dao;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.yoao.crm.DBManager.DBManager;
import org.yoao.moneymachine.bean.TgAdImages;

/**
 * @author Weishion.Yi
 * @description
 * @date 2010-10-4 下午03:12:13
 */
public class TgAdImagesDAO extends DBManager {
	private static final Logger log = Logger.getLogger(FileDAO.class);

	public List find(Map map) throws Exception {
		String sql = "select tgtopimages.id, tgtopimages.title, tgtopimages.url, tgtopimages.linkUrl "
				+ "from tgtopimages where 1=1";
		String where = "";
		if (map != null && !map.isEmpty()) {
			Iterator iter = map.keySet().iterator();
			while (iter.hasNext()) {
				String key = iter.next().toString();
				String value = map.get(key).toString();
				where = where + "tgtopimages." + key + "='" + value + "' and ";
			}
		}
		if (where.endsWith(" and "))
			where = where.substring(0, where.length() - 4);
		if (where.equals(""))
			where = null;
		if (where != null) {
			sql = sql + " and " + where;
		}
		return find(sql);
	}

	public List findAll() throws Exception {
		String sql = "select id, title, url, linkUrl from tgtopimages";
		return find(sql);
	}

	public TgAdImages findById(String id) throws Exception {
		String sql = "select  id, title, url, linkUrl from tgtopimages where id="
				+ id;
		return findSingle(querySingleRow(sql));
	}

	private List find(String sql) throws Exception {
		List list = queryMultiColumn(sql);
		List result = null;
		if (list != null && !list.isEmpty()) {
			result = new ArrayList();
			for (int i = 0; i < list.size(); i++) {
				Map map = (Map) list.get(i);
				TgAdImages ad = findSingle(map);
				if (ad == null)
					ad = new TgAdImages();
				result.add(ad);
			}
		}
		return result;
	}

	private TgAdImages findSingle(Map data) throws Exception {
		TgAdImages ad = null;
		if (data != null && !data.isEmpty()) {
			ad = new TgAdImages();
			ad.setId(Integer.parseInt(data.get("ID").toString()));
			ad.setTitle((String) data.get("TITLE"));
			ad.setUrl((String) data.get("URL"));
			ad.setLinkUrl((String) data.get("LINKURL"));
		}
		return ad;
	}

	public int insert(TgAdImages ad) throws Exception {
		String sql = "insert into tgtopimages(title, url, linkUrl)values("
				+ setNull(ad.getTitle()) + ", " + setNull(ad.getUrl()) + ", "
				+ setNull(ad.getLinkUrl()) + ")";
		return update(sql);
	}

	public int modify(TgAdImages ad) throws Exception {
		String sql = "update set tgtopimages title=" + setNull(ad.getTitle())
				+ ", url=" + setNull(ad.getUrl()) + ", linkUrl="
				+ setNull(ad.getLinkUrl()) + " where id=" + ad.getId();
		return update(sql);
	}

	public int delete(String id) throws Exception {
		String sql = null;
		if (id.indexOf(",") > 0)
			sql = "delete from tgtopimages where id in(" + id + ")";
		else
			sql = "delete from tgtopimages where id=" + id;
		return update(sql);
	}
}